﻿Imports System.Windows
Imports CommunityToolkit.Mvvm.ComponentModel
Imports CommunityToolkit.Mvvm.Input

Public Class NewBlogPageViewModel : Inherits ObservableObject

    Private _blog As Blog

    Public Property Blog As Blog
        Get
            Return _blog
        End Get
        Set(ByVal value As Blog)
            SetProperty(_blog, value)
        End Set
    End Property

    Private _songs As List(Of Song)
    Public Property Songs As List(Of Song)
        Get
            Return _songs
        End Get
        Set(ByVal value As List(Of Song))
            SetProperty(_songs, value)
        End Set
    End Property

    Private _searchText As String
    Public Property SearchText As String
        Get
            Return _searchText
        End Get
        Set(ByVal value As String)
            SetProperty(_searchText, value)
        End Set
    End Property

    Private _songSelected As Song
    Public Property SongSelected As Song
        Get
            Return _songSelected
        End Get
        Set(ByVal value As Song)
            SetProperty(_songSelected, value)
            'If value IsNot Nothing Then Console.WriteLine("SongSelected changed to " & value.Title)
        End Set
    End Property

    Private ReadOnly _client As HttpClientBase

    Public Property SearchSongsCommand As New AsyncRelayCommand(AddressOf SearchSongsClicked)
    Public Property PostBlogCommand As New AsyncRelayCommand(AddressOf PostBlogClicked)

    Public Sub New()
        _client = New HttpClientBase()
        Blog = New Blog()
        Songs = New List(Of Song)
    End Sub

    Private Async Function SearchSongsClicked() As Task
        Songs.Clear()
        SongSelected = Nothing
        If Not String.IsNullOrEmpty(SearchText) Then
            Try
                Dim response = Await _client.GetAsync(Of List(Of Song))(String.Format("vdm/api/music/search/{0}", SearchText))
                If response.Code = 200 Then
                    Songs = response.Data
                Else
                    MessageBox.Show("搜索失败：" + response.Message)
                End If
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Console.WriteLine(ex.StackTrace)
                MessageBox.Show("系统错误：" & ex.Message)
                NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
            End Try
        Else
            MessageBox.Show("请输入关键词后再进行搜索！")
        End If
    End Function

    Private Async Function PostBlogClicked() As Task
        If String.IsNullOrEmpty(Blog.Content) Then
            MessageBox.Show("请输入博客内容！")
            Return
        End If
        If SongSelected Is Nothing Then
            MessageBox.Show("请选择一首歌！")
            Return
        End If
        '上传漂流瓶到服务器
        With Blog
            .Author = Application.Current.Resources("UserName").ToString()
            .UpVotes = 0
            .DownVotes = 0
            .SongKeyword = SearchText
            .SongID = SongSelected.Id
            .PostDate = Date.Now
        End With
        Try
            Dim response = Await _client.PostAsync(Of Boolean)(Blog, "vdm/api/blog/createBlog")
            If response.Code = 200 And response.Data Then
                MessageBox.Show("漂流瓶已开始漂泊，等待有缘人的拾取！")
                NavigationService.Navigate(NavigationData.MAIN_LAYOUT_URI)
            Else
                MessageBox.Show($"上传漂流瓶失败：{response.Message}")
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.StackTrace)
            MessageBox.Show("系统错误：" & ex.Message)
            NavigationService.Navigate(NavigationData.LOGIN_PAGE_URI)
        End Try
    End Function

    'Private Sub OnSongSelectedChanged(songSeleted As Song)
    '    If songSeleted IsNot Nothing Then
    '        SongSelected = songSeleted
    '    End If
    'End Sub

End Class
